#include "StringTools.h"
#include <Windows.h>

string strtool::trim(const string& str)
{
	string::size_type pos = str.find_first_not_of(' ');
	if (pos == string::npos)
	{
		return str;
	}
	string::size_type pos2 = str.find_last_not_of(' ');
	if (pos2 != string::npos)
	{
		return str.substr(pos, pos2 - pos + 1);
	}
	return str.substr(pos);
}

wstring strtool::trim(const wstring& str)
{
	wstring::size_type pos = str.find_first_not_of(' ');
	if (pos == wstring::npos)
	{
		return str;
	}
	wstring::size_type pos2 = str.find_last_not_of(' ');
	if (pos2 != wstring::npos)
	{
		return str.substr(pos, pos2 - pos + 1);
	}
	return str.substr(pos);
}

int strtool::split(const string& str, vector<string>& ret_, string sep)
{
	if (str.empty())
	{
		return 0;
	}

	string tmp;
	string::size_type pos_begin = str.find_first_not_of(sep);
	string::size_type comma_pos = 0;

	while (pos_begin != string::npos)
	{
		comma_pos = str.find(sep, pos_begin);
		if (comma_pos != string::npos)
		{
			tmp = str.substr(pos_begin, comma_pos - pos_begin);
			pos_begin = comma_pos + sep.length();
		}
		else
		{
			tmp = str.substr(pos_begin);
			pos_begin = comma_pos;
		}

		if (!tmp.empty())
		{
			ret_.push_back(tmp);
			tmp.clear();
		}
	}
	return 0;
}

int strtool::split(const wstring& str, vector<wstring>& ret_, wstring sep)
{
	if (str.empty())
	{
		return 0;
	}

	wstring tmp;
	wstring::size_type pos_begin = str.find_first_not_of(sep);
	wstring::size_type comma_pos = 0;

	while (pos_begin != wstring::npos)
	{
		comma_pos = str.find(sep, pos_begin);
		if (comma_pos != wstring::npos)
		{
			tmp = str.substr(pos_begin, comma_pos - pos_begin);
			pos_begin = comma_pos + sep.length();
		}
		else
		{
			tmp = str.substr(pos_begin);
			pos_begin = comma_pos;
		}

		if (!tmp.empty())
		{
			ret_.push_back(tmp);
			tmp.clear();
		}
	}
	return 0;
}

vector<string> strtool::split(const string & str, string sep)
{
	vector<string> ret_;
	if (str.empty())
	{
		return ret_;
	}

	string tmp;
	string::size_type pos_begin = str.find_first_not_of(sep);
	string::size_type comma_pos = 0;

	while (pos_begin != string::npos)
	{
		comma_pos = str.find(sep, pos_begin);
		if (comma_pos != string::npos)
		{
			tmp = str.substr(pos_begin, comma_pos - pos_begin);
			pos_begin = comma_pos + sep.length();
		}
		else
		{
			tmp = str.substr(pos_begin);
			pos_begin = comma_pos;
		}

		if (!tmp.empty())
		{
			ret_.push_back(tmp);
			tmp.clear();
		}
	}
	return ret_;
}

vector<wstring> strtool::split(const wstring & str, wstring sep)
{
	vector<wstring> ret_;
	if (str.empty())
	{
		return ret_;
	}

	wstring tmp;
	wstring::size_type pos_begin = str.find_first_not_of(sep);
	wstring::size_type comma_pos = 0;

	while (pos_begin != wstring::npos)
	{
		comma_pos = str.find(sep, pos_begin);
		if (comma_pos != wstring::npos)
		{
			tmp = str.substr(pos_begin, comma_pos - pos_begin);
			pos_begin = comma_pos + sep.length();
		}
		else
		{
			tmp = str.substr(pos_begin);
			pos_begin = comma_pos;
		}

		if (!tmp.empty())
		{
			ret_.push_back(tmp);
			tmp.clear();
		}
	}
	return ret_;
}

string strtool::int2str(int i)
{
	char str[64];
	sprintf(str, "%d", i);
	return string(str);
}

string strtool::dou2str(double d, int decimalDigits)
{
	string format;
	format = "%." + int2str(decimalDigits) + "f";
	char str[64];
	sprintf(str, format.c_str(), d);
	return string(str);
}

int strtool::hexstr2int(string hexval)
{
	int ret;
	bool isHex;
	for (int i = 0; i < hexval.size(); i++)
	{
		auto isHex = hexval[i] >= '0' && hexval[i] <= '9'
			|| hexval[i] >= 'a' && hexval[i] <= 'f'
			|| hexval[i] >= 'A' && hexval[i] <= 'F';
		if (!isHex) return -1;
	}
	sscanf(hexval.c_str(), "%x", &ret);
	return ret;
}

wchar_t * strtool::char2wchar(const char * cstr)
{
	size_t len = strlen(cstr) + 1;
	size_t converted = 0;
	wchar_t *wstr;
	wstr = (wchar_t*)malloc(len * sizeof(wchar_t));
	mbstowcs_s(&converted, wstr, len, cstr, _TRUNCATE); 
	return wstr;
}

wchar_t* strtool::Utf82Unicode(const char* utf)
{
	if (!utf || !strlen(utf))
	{
		return NULL;
	}
	int dwUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, utf, -1, NULL, 0);
	size_t num = dwUnicodeLen * sizeof(wchar_t);
	wchar_t *pwText = (wchar_t*)malloc(num);
	memset(pwText, 0, num);
	MultiByteToWideChar(CP_UTF8, 0, utf, -1, pwText, dwUnicodeLen);
	//return (char*)pwText;
	return pwText;
}

char* strtool::Unicode2Utf8(const char* unicode)
{
	int len;
	len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL);
	char *szUtf8 = (char*)malloc(len + 1);
	memset(szUtf8, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL);
	return szUtf8;
}